#include <iostream>
#include <cmath>
#include <string.h>
#include <algorithm>
using namespace std;

const int N = 1e4 + 5;

bool depend[N], appear[N];
int hpnum[N];
int pos;

struct Hp
{
    int num, val;
    bool operator<(const Hp &h) const
    {
        return this->num < h.num;
    }
} hps[N];

int isPrime(int val)
{
    for (int i = 2; i <= sqrt(val); i++)
    {
        if (!(val % i))
        {
            return 1;
        }
    }
    return 2;
}

int main()
{
    int left, right, flag = 0;
    cin >> left >> right;
    for (int i = left; i <= right; i++)
    {
        memset(appear, false, sizeof(appear));
        int temp = i;
        while (temp != 1)
        {
            int next = 0;
            while (temp)
            {
                next += (pow(temp % 10, 2));
                temp /= 10;
            }
            temp = next;
            if (appear[next])
            {
                // 循环了
                hpnum[i] = 0;
                break;
            }
            appear[next] = true;
            depend[next] = true;
            hpnum[i]++;
        }
        if (temp == 1)
        {
            flag = 1;
            hps[pos].num = i;
            hps[pos].val = isPrime(i) * hpnum[i];
            pos++;
        }
    }
    sort(hps, hps + pos);
    if (flag)
    {
        for (int i = 0; i < pos; i++)
        {
            if (!depend[hps[i].num])
            {
                cout << hps[i].num << " " << hps[i].val << endl;
            }
        }
    }
    else
    {
        cout << "SAD" << endl;
    }
    return 0;
}